/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.generate.mybatisplus.handler;

import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.hyts.generate.mybatisplus.bean.AttributePackageBean;
import com.hyts.generate.mybatisplus.bean.AttributeStrategryBean;
import com.hyts.generate.mybatisplus.bean.GenerateBean;
import com.hyts.generate.mybatisplus.config.DefaultAttributeConfig;
import com.hyts.generate.mybatisplus.handler.base.AbstractAttributeValueHandler;
import com.hyts.generate.mybatisplus.primtive.MpgConfiguration;

import java.util.Objects;
import java.util.Optional;

/**
 * @project-name:standard-boot
 * @package-name:com.hyts.standard.boot.generator.handler
 * @author:LiBo/Alex
 * @create-date:2019-07-26 15:15
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class AttributeStategryHandler extends AbstractAttributeValueHandler<AttributeStrategryBean> {

    /**
     * 操作构造器
     * @param generateBean
     */
    public AttributeStategryHandler(GenerateBean generateBean) {
        super(generateBean);
        Objects.requireNonNull(Objects.isNull(generateBean),"操作服务创建对象不可以为空!");
    }

    /**
     * 构建方法操作
     * @param param
     * @return
     */
    @Override
    public GenerateBean build(AttributeStrategryBean param) {
        Optional<StrategyConfig> strategyConfig =  Optional.of(param).map(param2->{
            StrategyConfig strategyConfig1 = new StrategyConfig();
            strategyConfig1.setCapitalMode((Boolean) getValue(param.isCapitalModel(), MpgConfiguration.STRATEGY_CONFIG.isCapitalMode()));
            strategyConfig1.setTablePrefix(new String[]{DefaultAttributeConfig.DEFAULT_TAB_PREFIX});
            strategyConfig1.setNaming(NamingStrategy.underline_to_camel);
            if(param.getIncludeTables() != null && param.getIncludeTables().size() != 0){
                strategyConfig1.setInclude((String[]) param.getIncludeTables().toArray());
            }
            if(param.getExcludeTables() != null && param.getExcludeTables().size() != 0){
                strategyConfig1.setExclude((String[]) param.getExcludeTables().toArray());
            }
            strategyConfig1.setEntityLombokModel((Boolean) getValue(param.isAddEntityLombokBuilderStyle(),true));
            strategyConfig1.setRestControllerStyle((Boolean) getValue(param.isRestController(),true));
            strategyConfig1.setEntityColumnConstant(false);
            strategyConfig1.setEntityBuilderModel((Boolean) getValue(param.isAddEntityLombokStyle(),true));
            strategyConfig1.entityTableFieldAnnotationEnable((Boolean) getValue(param.isEntityTableFieldAnnotationEnable(),true));
            return strategyConfig1;
        });
        return generateBean.setStrategyConfig(strategyConfig.get());
    }
}
